#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "newgame.h"
#include "game.h"
#include "nation.h"
#include "nations.h"
#include "csv_read.h"
#include "heat.h"
#include "heat_rider.h"
#include <iostream>
#include <QVector>
#include <QProcess>
#include "time.h"
#include "csv.h"
#include "dmp.h"
#include "team.h"
#include "load_save.h"
#include "match.h"
#include "database.h"
#include <iostream>
using namespace std;




MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ///////////////////////////////////////////////
    db.load();

    //////////////////////////////////////////////
    srand((unsigned)time(0));
    ui->setupUi(this);
    setWindowTitle("Hot Tires");
    combos_a << ui->comb_a0 << ui->comb_a1 << ui->comb_a2 << ui->comb_a3
                << ui->comb_a4 << ui->comb_a5 << ui->comb_a6 << ui->comb_a7;
    combos_h << ui->comb_h0 << ui->comb_h1 << ui->comb_h2 << ui->comb_h3
                << ui->comb_h4 << ui->comb_h5 << ui->comb_h6 << ui->comb_h7;
    combos_heat << ui->comboBox_rider1 << ui->comboBox_rider2 << ui->comboBox_rider3
                << ui->comboBox_rider4;

     /*
        QStringList rowData, rowOfData;
            csv_read reader("nations.csv");
            rowOfData = reader.read();
            gamer.nations=new nation[rowOfData.size()];
            gamer.numb_of_nations=rowOfData.size()-1;

          // ui->name_label->setText();

            for (int x = 0; x < rowOfData.size()-1; x++)
            {
                rowData = rowOfData.at(x).split(",");


                gamer.nations[x].nazwa=rowData[1];
                gamer.nations[x].icon.addFile("flags/"+gamer.nations[x].nazwa+".bmp" );



            }
       */
    //load_country();

    //!!!! cos tu okomentowalem jak dobrze pamietam -Kinoll
}

MainWindow::~MainWindow()
{
    delete ui;
}
void MainWindow::fillDmp()
{
    QStringList fill1 = db.dmps[0].teams[0]->getRidersTextRepresenatation();
    QStringList fill2 = db.dmps[0].teams[1]->getRidersTextRepresenatation();

    for (int i = 0; i < combos_a.size(); i++)
    {
        combos_a[i]->insertItems(0, fill2);
        combos_h[i]->insertItems(0, fill1);
    }
    combos_a[7]->setHidden(true);
    combos_h[7]->setHidden(true);
}
void MainWindow::fillCurrentHeat(QString type, int players_team = 4)
{
    QStringList fill1;
    int players_team_last_index;

    for (int i = 0; i < combos_heat.size(); i++)
        combos_heat[i]->clear();

    if (type == "dmp")
    {
        fill1 = db.dmps[0].getRidersTextRepresentation();

        if (players_team == 0)
            players_team_last_index = 8;
        else
            players_team_last_index = 30;

        for (int i = 0; i < combos_heat.size(); i++)
        {
            if(db.dmps[0].table_of_heats.heats[db.dmps[0].heat_number].start_positions[i].rider_number < players_team_last_index)
            {
                combos_heat[i]->insertItems(0, fill1);
                combos_heat[i]->setDisabled(false);
            }
            else
            {
                combos_heat[i]->insertItem(0, db.dmps[0].riders_list[db.dmps[0].table_of_heats.heats[db.dmps[0].heat_number].start_positions[i].rider_number]->surname);
                combos_heat[i]->setDisabled(true);
            }
        }
        for (int i = 0; i < 4; i++)
            if (combos_heat[i]->isEnabled())
                combos_heat[i]->setCurrentIndex(db.dmps[0].table_of_heats.heats[db.dmps[0].heat_number].start_positions[i].rider_number);
    }
    else if (type == "ind16")
        for (int i = 0; i < 4; i++)
        {
            combos_heat[i]->clear();
            combos_heat[i]->setDisabled(true);
            combos_heat[i]->insertItem(0, db.ind16s[0].riders_list[db.ind16s[0].table_of_heats.heats[db.ind16s[0].heat_number].start_positions[i].rider_number]->surname);
        }
}

void MainWindow::on_but_new_clicked()
{
   newgame *newg = new newgame;
   newg->exec();
   ui->tab_Manager->setEnabled(true);
   ui->tab_Mmain->setCurrentIndex(0);
   ui->tab_Manager->setCurrentIndex(0);
   QString change = gamer.player.name+" "+gamer.player.surname;
   ui->name_label->setText(change);
}

void MainWindow::on_but_load_clicked()
{
    ui->tab_Manager->setEnabled(true);
    ui->tab_Mmain->setCurrentIndex(0);
    ui->tab_Manager->setCurrentIndex(0);
}

void MainWindow::on_name_label_textChanged()
{

}

void MainWindow::on_but_heat_clicked()

{
}

void MainWindow::on_but_match_clicked()
{
}

void MainWindow::on_but_team_apply_clicked()
{
    while (true)
    {
        db.dmps[0].containers[0].is_correct = true;
        db.dmps[0].containers[1].is_correct = true;
        db.dmps[0].containers[0].riders.clear();
        db.dmps[0].containers[1].riders.clear();

        for (int i = 0; i < combos_h.size(); i++)
            if (combos_h[i]->currentText() != "")
                db.dmps[0].containers[0].add(db.dmps[0].teams[0]->persons[combos_h[i]->currentIndex()]);
        for (int i = 0; i < combos_a.size(); i++)
            if (combos_a[i]->currentText() != "")
                db.dmps[0].containers[1].add(db.dmps[0].teams[1]->persons[combos_a[i]->currentIndex()]);
        if (!(db.dmps[0].containers[0].check("dmp", db.date) && db.dmps[0].containers[1].check("dmp", db.date)))
        {
            break;
        }
        else
            db.dmps[0].mergeContainers(db.dmps[0].containers);
            for (int i = 0; i < combos_a.size(); i++)
            {
                combos_a[i]->clear();
                combos_h[i]->clear();
            }
            fillCurrentHeat("dmp", 0);
            ui->tab_match_2->setCurrentIndex(0);
            break;
    }

}

void MainWindow::on_pushButton_clicked()
{
    if (e.event_type == "dmp" && db.dmps[0].heat_number < 15)
    {
        for (int i = 0; i < 4; i++)
        {
        if ((combos_heat[i]->isEnabled()) && (combos_heat[i]->currentIndex() != db.dmps[0].table_of_heats.heats[db.dmps[0].heat_number].start_positions[i].rider_number))
            if (db.is_home)
            {
                if (db.dmps[0].isChangeValid(combos_heat[i]->currentIndex(), i))
                {
                    db.dmps[0].table_of_heats.heats[db.dmps[0].heat_number].start_positions[i].rider_number = combos_heat[i]->currentIndex();
                }
                else
                    return;
            }
        }
        txt = db.dmps[0].runHeat(db.riders);
        ui->textBrowser_heatDisplay->insertHtml(txt.join(" ")+"<br>");
        ui->textBrowser_matchHeader->setPlainText(db.dmps[0].teams[0]->name+ " " + QString::number(db.dmps[0].standings_m.team_points[15][0])
                + " : " + QString::number(db.dmps[0].standings_m.team_points[15][1])+ " " + db.dmps[0].teams[1]->name);
        if (db.dmps[0].heat_number != 15)
            fillCurrentHeat("dmp", 0);
    }
    else if (e.event_type == "ind16" && db.ind16s[0].heat_number < 20)
    {
        txt = db.ind16s[0].runHeat(db.riders);
        ui->textBrowser_heatDisplay->insertHtml(txt.join(" ")+"<br>");
        if (db.ind16s[0].heat_number != 20)
            fillCurrentHeat("ind16", 4);
    }
    else
    {
        if (e.event_type == "dmp")
        {
            for (int i = 0; i < db.dmps[0].riders_list.size(); i++)
                if ((i != 7) && (i != 15))
                {
                    QStringList tempHist;
                    tempHist << db.dmps[0].date.toString();
                    for (int k = 0; k < db.dmps[0].standings_m.rider_points[i].size(); k++)
                        tempHist.append(QString::number(db.dmps[0].standings_m.rider_points[i][k]));
                    db.dmps[0].riders_list[i]->history << tempHist;
                }
            db.dmps.removeFirst();
        }
        else if (e.event_type == "ind16")
        {
            for (int i = 0; i < db.ind16s[0].riders_list.size(); i++)
            {
                QStringList tempHist;
                tempHist << db.ind16s[0].date.toString();
                for (int k = 0; k < db.ind16s[0].standings_m.rider_points[i].size(); k++)
                    tempHist.append(QString::number(db.ind16s[0].standings_m.rider_points[i][k]));
                db.ind16s[0].riders_list[i]->history << tempHist;
            }
            db.ind16s.removeFirst();
        }
        ui->textBrowser_heatDisplay->setHtml("");
        ui->pushButton->setDisabled(true);
        ui->but_continue->setDisabled(false);
    }

}

void MainWindow::on_but_continue_clicked()
{
    ui->name_label->setText(db.date.toString());

    if ((db.events.isEmpty() == false) && (db.events[0].date == db.date))
    {
        ui->pushButton->setDisabled(false);
        e = db.events.takeFirst();
        ui->but_continue->setDisabled(true);
        if (e.event_type == "dmp")
        {
            fillDmp();
            ui->tab_Mmain->setCurrentIndex(2);
            ui->tab_match_2->setCurrentIndex(1);
        }
        else if (e.event_type == "ind16")
        {
            fillCurrentHeat("ind16", 4);
            ui->tab_Mmain->setCurrentIndex(2);
            ui->tab_match_2->setCurrentIndex(0);
        }
    }
    else
        db.date = db.date.addDays(1);
}

void MainWindow::on_comboBox_rider1_currentIndexChanged(const QString &arg1)
{

}
